package io.renren.modules.common.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.common.entity.TradeClassifyEntity;
import io.renren.modules.common.entity.TradeJobEntity;
import io.renren.modules.common.service.TradeClassifyService;
import io.renren.modules.common.service.TradeJobService;
import io.renren.modules.common.viewdo.TreeOption;
import io.renren.modules.companys.dao.CompanyDao;
import io.renren.modules.companys.entity.CompanyEntity;
import io.renren.modules.workjobs.dao.WorkJobDao;
import io.renren.modules.workjobs.entity.WorkJobEntity;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.common.entity.TradeCategoryEntity;
import io.renren.modules.common.service.TradeCategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author nyc
 * @email 2691778746@qq.com
 * @date 2022-03-08 17:45:36
 */
@RestController
@RequestMapping("common/tradecategory")
public class TradeCategoryController {
    @Autowired
    private TradeCategoryService tradeCategoryService;
    @Autowired
    private TradeClassifyService tradeClassifyService;
    @Autowired
    private TradeJobService tradeJobService;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private WorkJobDao workJobDao;


    @PostMapping("/resCCJInfo")
    public R resCCJInfo(@RequestBody String[] ids){

        String tradeCategoryCode = ids[0];
        String tradeClassifyCode = ids[1];
        String tradeJobCode = ids[2];

        QueryWrapper<TradeCategoryEntity> tradeCategoryWrapper = new QueryWrapper<>();
        QueryWrapper<TradeClassifyEntity> tradeClassifyWrapper = new QueryWrapper<>();
        QueryWrapper<TradeJobEntity> tradeJobWrapper = new QueryWrapper<>();

        tradeCategoryWrapper.eq("trade_id",tradeCategoryCode);
        tradeClassifyWrapper.eq("classify_id",tradeClassifyCode);
        tradeJobWrapper.eq("job_type_id",tradeJobCode);

        TradeCategoryEntity tradeCategoryEntity = tradeCategoryService.getOne(tradeCategoryWrapper);
        TradeClassifyEntity classifyEntity = tradeClassifyService.getOne(tradeClassifyWrapper);
        TradeJobEntity tradeJobEntity = tradeJobService.getOne(tradeJobWrapper);

        String[] ccjRes =new String[3];

        ccjRes[0] = tradeCategoryEntity.getTradeName();
        ccjRes[1] = classifyEntity.getClassifyName();
        ccjRes[2] = tradeJobEntity.getJobName();

        return R.ok().put("data",ccjRes);
    }

    @GetMapping("/trade-category")
    public R industoryTwoSelectTreeOption(){
        List<TreeOption> listData = tradeCategoryService.treeOptionCCJ();
        return R.ok().put("data",listData);
    }



    /*提供给客户端 前端调用的方法  暂时没有用到*/
    @GetMapping("/trade-category-company/{userId}")
    public R returnCompanyTwoSelect(@PathVariable("userId")String userId){
        List<String> resList = new ArrayList<>();
        QueryWrapper<CompanyEntity> companyWrapper = new QueryWrapper<>();
        companyWrapper.eq("create_user_id",userId);
        CompanyEntity companyEntity = companyDao.selectOne(companyWrapper);
        resList.add(companyEntity.getTradeId().toString());
        resList.add(companyEntity.getClassifyId().toString());
        return R.ok().put("data",resList);
    }

    /*提供给客户端调用的方法 职位所属行业*/
    @GetMapping("/trade-category-industory/{workJobId}")
    public R returnWorkJonTwoSelect(@PathVariable("workJobId")String workJobId){
        List<String> resList = new ArrayList<>();
        QueryWrapper<WorkJobEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",workJobId);
        WorkJobEntity workJobEntity = workJobDao.selectOne(wrapper);
        resList.add(workJobEntity.getTradeId().toString());
        resList.add(workJobEntity.getClassifyId().toString());
        return R.ok().put("data",resList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("common:tradecategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tradeCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("common:tradecategory:info")
    public R info(@PathVariable("id") Integer id){
		TradeCategoryEntity tradeCategory = tradeCategoryService.getById(id);

        return R.ok().put("tradeCategory", tradeCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("common:tradecategory:save")
    public R save(@RequestBody TradeCategoryEntity tradeCategory){
		tradeCategoryService.save(tradeCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("common:tradecategory:update")
    public R update(@RequestBody TradeCategoryEntity tradeCategory){
		tradeCategoryService.updateById(tradeCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("common:tradecategory:delete")
    public R delete(@RequestBody Integer[] ids){
		tradeCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
